const fs = require('fs');
const path = require('path');
const buildJs = require("./builder.js");
let xls = require('node-xlsx');
console.log("argv",process.argv)
var readtag;
if(process.argv.length>2)
{
    readtag = process.argv[2];
}
let config = fs.readFileSync(path.join(__dirname,'./wdConfig.json'), 'utf8');
let res=null;
config = JSON.parse(config);
fs.readdir("./script/",function(err,files){
    if(err) throw err;
    console.log(files)
    for (var i=0; i<files.length; i++)
    {
        if(files[i].indexOf("config.xlsx")!=-1)
        {
            var obj = readXls(path.join("script/",files[i]));
            config.android.platform.creatorPath=obj.creatorPath.value;
            config.android.platform.projectPath=obj.projectPath.value;
            config.android.platform.appkey=obj.appkey.value;
            config.android.platform.appId=obj.appId.value;
            config.android.platform.secret=obj.secret.value;
            config.android.platform.appName=obj.appName.value;
            config.android.platform.packageName=obj.packageName.value;
            config.android.platform.buildPath=obj.buildPath.value;
            config.android.platform.apiLevel=obj.apiLevel.value;
            config.android.platform.portrait=obj.portrait.value;
            config.android.csjad.enable=obj.csjad.value;
            config.android.oppoad.enable=obj.oppoad.value;
            config.android.vivoad.enable=obj.vivoad.value;
            config.android.marsad.enable=obj.marsad.value;
            config.android.csjad.adId.rewardVideoId=obj.rewardVideoId .value;
            config.android.csjad.adId.fullScreenVideoId=obj.fullScreenVideoId.value;
            config.android.csjad.adId.bannerId=obj.bannerId.value;
            config.android.csjad.adId.splashId=obj.splashId.value;
            config.android.csjad.adId.feedId=obj.feedId.value;
            config.android.csjad.adId.appid=obj.ADID.value;
            config.android.csjad.adId.insertId=obj.insertId.value;
            config.android.analytics[0].enable=obj.umeng.value;
            config.android.analytics[1].enable=obj.tracking.value; 
            config=JSON.stringify(config);
            fs.writeFile(path.join(__dirname,'./wdConfig.json'), config, 'utf8', (err) => {
                if (err) throw err;
                    buildJs.init();
            });         
        }
    }
});


function readXls(fileName)
{
    console.log("读取",fileName)
    let xlsObj = xls.parse(fileName);
    var sheet;
    if(readtag)
    {
        for(var i=0;i<xlsObj.length;i++)
        {
            if(xlsObj[i].name.indexOf(readtag)!=-1)
            {
                sheet = xlsObj[i].data;
                break;
            }
        }
    }else
    {
        sheet = xlsObj[0].data;
    }
    if(sheet)
    {
        var needReads = getNeedRead(sheet[0]);
        if(needReads.length<=0)return null;
        var data = {};
        var types = getReadTypes(sheet[1],needReads);
        var names = getParamName(sheet[2],needReads);
        for(var i=4;i<sheet.length;i++)
        {
            var row = sheet[i];
            var key = row[needReads[0]];
			if(key==undefined)
			{
				continue;
			}
            var outrow = data[key] = {};
            for(var j=0;j<needReads.length;j++)
            {
                var value = row[needReads[j]];
                var type = types[j];
                var key = names[j];
                if(type.indexOf("[][]")!=-1)
                {
                    if(value)
                    {
                        var arr = value.split("][");
                        var len = arr.length;
                        arr[0] = arr[0].slice(1);
                        arr[len - 1] = arr[len - 1].slice(0,arr[len - 1].length - 1);
                        for (var k=0; k<len; k++)
                        {
                            arr[k] = arr[k].split(",");
							//测试--
							if(type.indexOf('int') != -1){
								for(let i = 0, iMax = arr[k].length; i < iMax; ++i){
									arr[k][i] = Number(arr[k][i]);
								}
							}
							//end--
                        }
                        outrow[key] = arr;
                        // var stra = value.split("][");
                        // var newstr = stra.join("],[");
                        // var tempjson = JSON.parse('{"arr":'+newstr+'}')
                        // outrow[key] = tempjson.arr;
                    }else
                    {
                        outrow[key] = [[]];
                    }
                }else if(type.indexOf("[]")!=-1)
                {
                    if(value)
                    {
                        outrow[key] = (value + "").split("|");
						//测试--
						if(type.indexOf('int') != -1){
							for(let i = 0, iMax = outrow[key].length; i < iMax; ++i){
								outrow[key][i] = Number(outrow[key][i]);
							}
						}
						//end--
                    }else
                    {
                        outrow[key] = [];
                    }
                }else
                {
                    if(value==undefined)
                    {
                        if(type.indexOf("string")!=-1)
                        {
                            outrow[key] = "";
                        }else
                        {
                            outrow[key] = 0;
                        }
                    }else
                    {
                        outrow[key] = value;
                    }
                }
            }
        }
        return data;
    }
    return null;
}

function getNeedRead(data)
{
    var needs = [];
    for(var i=0;i<data.length;i++)
    {
        if(data[i]&&(data[i].indexOf("c")!=-1||data[i].indexOf("C")!=-1))
        {
            needs.push(i);
        }
    }
    return needs;
}

function getReadTypes(data,needs)
{
    var types = [];
    for(var i=0;i<needs.length;i++)
    {
        types.push(data[needs[i]])
    }
    return types;
}

function getParamName(data,needs)
{
    var names = [];
    for(var i=0;i<needs.length;i++)
    {
        names.push(data[needs[i]])
    }
    return names;
}